#include <iostream>
#include <sw/redis++/redis++.h>
#include <vector>
#include <string>
#include "PPrint.hpp"
#include <chrono>
#include <set>

using std::cout;
using std::endl;
using std::string;
using std::unordered_map;
using std::vector;

using std::set;
using sw::redis::Redis;
using namespace std::chrono_literals;

void test1(Redis &redis)
{
    std::cout << "sadd and smembers" << std::endl;
    redis.flushall();

    redis.sadd("key", "111");
    redis.sadd("key", {"222", "333", "444"});

    set<string> elems = {"555", "666", "777"};
    redis.sadd("key", elems.begin(), elems.end());

    vector<string> result;
    auto it = std::inserter(result, result.end());
    redis.smembers("key", it);

    printContainer(result);
}

void test2(Redis &redis)
{
    std::cout << "sismember and len" << std::endl;
    redis.flushall();

    redis.sadd("key", {"111", "222", "333", "444"});

    bool result = redis.sismember("key", "444");
    cout << result << endl;
    result = redis.sismember("key", "555");
    cout << result << endl;

    cout << redis.scard("key") << endl;
}

void test3(Redis &redis)
{
    std::cout << "spop" << std::endl;
    redis.flushall();

    redis.sadd("key", {"111", "222", "333", "444"});
    auto result = redis.spop("key");

    cout << result.value() << endl;

    result = redis.spop("key1");
    cout << result.value() << endl;
}

void test4(Redis &redis)
{
    std::cout << "sinter" << std::endl;
    redis.flushall();

    redis.sadd("key1", {"111", "222", "333", "444"});
    redis.sadd("key2", {"111", "222", "555", "666"});

    set<string> result;
    auto it = std::inserter(result, result.end());
    redis.sinter({"key1", "key2"}, it);

    printContainer(result);

    cout << "----------------------------" << endl;
    result.clear();
    redis.sdiff({"key1", "key2"}, it);
    printContainer(result);
}

int main()
{
    sw::redis::Redis redis("tcp://127.0.0.1");
    test4(redis);
    return 0;
}